package com.ruge.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruge.mapper.DataBaseMapper;
import com.ruge.mapper.RugeColCommentsMapper;
import com.ruge.model.RugeColComments;
import com.ruge.service.DataBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class DataBaseServiceImpl implements DataBaseService {

    @Resource
    private RugeColCommentsMapper rugeColCommentsMapper;

    @Resource
    private DataBaseMapper dataBaseMapper;

    // 查询 ORACLE表注释
    @Override
    public Map<String, Object> getColComments(Map<String, String> params) {
        Map<String, Object> map = new HashMap<>();
        String page = params.getOrDefault("page", "1");
        String rows = params.getOrDefault("rows", "20");
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<JSONObject> jsonObjects = dataBaseMapper.pageListColComments(params);
        for (JSONObject jsonObject : jsonObjects) {
            LambdaQueryWrapper<RugeColComments> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RugeColComments::getRugeColumnName, jsonObject.getString("COLUMN_NAME"));
            RugeColComments rugeColComments = rugeColCommentsMapper.selectOne(wrapper);
            if (rugeColComments != null) {
                jsonObject.put("rugeComments", rugeColComments.getRugeComments());
            } else {
                jsonObject.put("rugeComments", "");
            }
        }
        map.put("data", new PageInfo<>(jsonObjects));
        map.put("msg", "查询成功");
        return map;
    }


    // 修改 ORACLE表注释
    @Override
    public Map<String, Object> updateColComments(Map<String, String> params) {
        Map<String, Object> map = new HashMap<>();
        String columnName = params.get("COLUMN_NAME");
        String comments = params.get("COMMENTS");
        rugeColCommentsMapper.deleteById(columnName);
        rugeColCommentsMapper.insert(RugeColComments.builder().rugeColumnName(columnName).rugeComments(comments).build());
        List<JSONObject> data = dataBaseMapper.preUpdateColComments(params);
        for (JSONObject datum : data) {
            datum.put("RUGE_COMMENTS", comments);
            try {
                dataBaseMapper.updateColComments(datum);
            } catch (Exception e) {
                try {
                    dataBaseMapper.purgeTable(datum.getString("TABLE_NAME").trim());
                } catch (Exception ex) {
//                    dataBaseMapper.dropTable(datum.getString("TABLE_NAME"));
                  //  dataBaseMapper.purgeTable(datum.getString("TABLE_NAME"));
                    ex.printStackTrace();
                }
            }
        }
        map.put("data", data);
        map.put("msg", "操作成功");
        return map;
    }
}
